package com.parser;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.senditcertified.mail.Attachment;

import android.text.Html;
import android.util.Log;
import android.widget.Toast;

public class Parser_Soap {
	String TOKEN_GetLoginTokenResult = "GetLoginTokenResult";
	String TOKEN_GetTimeResult = "GetTimeResult";
	String TOKEN_GetUserResult = "GetUserResult";
	String TOKEN_GetUserImage = "GetUserSecurityImageResult";
	String TOKEN_GetUserInbox = "GetUserInboxResult";
	

	public String GetLoginToken(InputStream instream,Error_Report report) {

		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);

			if (doc != null) {
				NodeList nl = doc
						.getElementsByTagName(TOKEN_GetLoginTokenResult);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetLoginTokenResult " + responseCode);
				}

			}
		} catch (Exception e) {
			report.setMessage("Illegal Exception");
		}
		return responseCode;

	}
	
	public String GetLoginTokenExpire(InputStream instream,Error_Report report) {

		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);

			if (doc != null) {
				NodeList nl = doc
						.getElementsByTagName("InvalidateLoginTokenResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("InvalidateLoginTokenResult " + responseCode);
				}

			}
		} catch (Exception e) {
			report.setMessage("Illegal Exception");
		}
		return responseCode;

	}
	
	public String GetFileUploadResponse(InputStream instream,Error_Report report) {

		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			 
			if (doc != null) {
				NodeList nl = doc
						.getElementsByTagName("GetFileFromVault_ExtResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetFileFromVault_ExtResult: " + responseCode);
				}

			}
		} catch (Exception e) {
			report.setMessage("Illegal Exception");
		}
		return responseCode;

	}
	
	
	public String GetFile(InputStream instream,Error_Report report) {

		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);

			if (doc != null) {
				NodeList nl = doc
						.getElementsByTagName("GetFileResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetFileResult " + responseCode);
				}

			}
		} catch (Exception e) {
			report.setMessage("Illegal Exception");
		}
		return responseCode;

	}

	public String GetTime(InputStream instream) {
		String responseCode = "";
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetTimeResult);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("Gettime" + responseCode);
				}

			}
		} catch (Exception e) {

		}
		return responseCode;
	}

	public String GetUser(InputStream instream,Error_Report repot) {
		String responseCode = "";
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetUserResult);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetUserResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repot.setMessage("Ilegal Exception");
		}
		return responseCode;
	}
	
	public String GetUserImage(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetUserImage);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetUserSecurityImageResult" + responseCode);
				}

			}
			if(responseCode==null){
				repor.setMessage("User not exit.");
				return responseCode="";
			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetUserInbox(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetUserInbox);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetUserSecurityImageResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	
	public String GetDeliveryAcceptance(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetDeliveryAcceptanceResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetDeliveryAcceptanceResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetVaultStatistics(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetVaultStatisticsResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetVaultStatistics" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetVaultCatelog(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetVaultCatalogResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetVaultCatelog" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetIsLoginTokenValid(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetIsLoginTokenValidResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetIsLoginTokenValidResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String RequestAccessCode(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("RequestAccessCodeResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("RequestAccessCodeResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetEPackage(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetEPackageResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("RequestAccessCodeResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	
	public String GetEPackageError(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetEPackageErrorResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("RequestAccessCodeResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	
	public String CopyFileToVault(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("CopyFileToVaultResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					
					System.out.println("CopyFileToVaultResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String GetWebInterfaceURL(InputStream instream, Error_Report repor) {
		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName("GetWebInterfaceURLResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetWebInterfaceURLResult" + responseCode);
				}

			}
		} catch (Exception e) {
			repor.setMessage("Internal service error");
		}
		return responseCode;
	}
	
	public String parseDeleteFile(InputStream instream,Error_Report repot) {
		String responseCode = "";
		try {
			/*DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetUserResult);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetUserResult" + responseCode);
				}

			}*/
		} catch (Exception e) {
			repot.setMessage("Ilegal Exception");
		}
		return responseCode;
	}
	
	public String parseDownloaadFile(InputStream instream,Error_Report repot) {
		String responseCode = "";
		try {
			/*DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);
			// String responseCode = "";
			if (doc != null) {
				NodeList nl = doc.getElementsByTagName(TOKEN_GetUserResult);
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					System.out.println("GetUserResult" + responseCode);
				}

			}*/
		} catch (Exception e) {
			repot.setMessage("Ilegal Exception");
		}
		return responseCode;
	}
	
	public String parseFileSize(InputStream instream,Error_Report report) {

		String responseCode = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(instream);

			if (doc != null) {
				NodeList nl = doc
						.getElementsByTagName("GetFileSizeFromVaultResult");
				if (nl.getLength() > 0) {
					Node node = nl.item(0);
					responseCode = node.getTextContent();
					Log.e("GetFileSizeFromVaultResult responseCode" , responseCode);
					try {
						JSONObject jsonObj = new JSONObject(responseCode);
						
						if(jsonObj.getString("ReturnValue")!=null){
							responseCode = jsonObj.getString("ReturnValue");
							Log.e("GetFileSizeFromVaultResult parsed responseCode" , responseCode);
						}
						else{
							responseCode = null;
						}
				}catch(Exception e){
					e.printStackTrace();
					responseCode = null;
				}
				
			}
		  }
		} catch (Exception e) {
			report.setMessage("Illegal Exception");
		}
		return responseCode;

	}
	
}
